package com.example.pro1;
import static androidx.constraintlayout.helper.widget.MotionEffect.TAG;

import com.example.pro1.MyTools;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.util.Pair;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

//改写继承父类
public class RateListActivity2 extends ListActivity implements Runnable{
    Handler boxAllRateList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //初始化放在子线程开启之前
        UpdateList();
        Thread RateListT = new Thread(this);
        RateListT.start();
    }
    public void UpdateList() {
        boxAllRateList = new Handler(Looper.myLooper()) {
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                if (msg.what == 5) {
                    //如果有消息返回，更新列表，建议不要用全局，因为大概率出错
                    List<String> info = (List<String>) msg.obj;
                    //适配器
                    ListAdapter adapter = new ArrayAdapter<String>(RateListActivity2.this, android.R.layout.simple_list_item_1, info);
                    setListAdapter(adapter);
                }
            }
        };
    }
    public void run() {
        Log.i(TAG, "run: this is childProject");
        String tarUrlRateList = "https://www.huilvzaixian.com/";
        ArrayList<String> infoTmp = new ArrayList<>();
        try {
            Document tarDocRateList = Jsoup.connect(tarUrlRateList).get();
            Element tarRateListTot = tarDocRateList.getElementsByClass("history-urls").get(0);
            Elements tarRateList = tarRateListTot.getElementsByTag("a");
            for (Element tarRateListOne:tarRateList) {
                Log.i(TAG, "run: " + tarRateListOne.text());
                String[] tarInfoRateList = tarRateListOne.text().split(" ");
                infoTmp.add(tarInfoRateList[1] + "-->" + tarInfoRateList[2]);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        Message msg = boxAllRateList.obtainMessage(5, infoTmp);
        boxAllRateList.sendMessage(msg);
    }
}